import { getProfileAPI, loginAPI } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'

const state = {
  token: getToken(),
  userInfo: {}
}

const mutations = {
  setToken(state, payload) {
    state.token = payload
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  },
  removeToken(state) {
    state.token = ''
  }

}

const actions = {
  // 登录
  async login({ commit }, payload) {
    // 调用接口
    const res = await loginAPI(payload)
    // 持久存储
    setToken(res.token)
    // vueX存储
    commit('setToken', res.token)
  },
  // 下拉列表
  async getUserInfo({ commit }) {
    const res = await getProfileAPI()
    commit('setUserInfo', res)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

